﻿package mbalib.docviewer
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	import mbalib.ui.constants.BUIConstants;
	import mbalib.ui.constants.ScrollPolicy;
	import mbalib.ui.controls.*;
	import mbalib.ui.core.*;
	import mbalib.ui.events.*;
	
	public class ScrollPane extends BUI
	{
		protected var _contentHeight:Number = 0;
		protected var contentContainer:Sprite;//放置内容的面板
		protected var _availableHeight:Number;
		protected var _availableWidth:Number;
		protected var _hScrollBar:ScrollBar;
		protected var defaultLineScrollSize:Number = 4;
		protected var _hPageScrollSize:Number = 0;
		protected var contentScrollRect:Rectangle;
		protected var background:BSkin;
		protected var _vPageScrollSize:Number = 0;
		protected var disabledOverlay:BSkin;
		protected var contentWidth:Number = 0;
		protected var _vScrollBar:ScrollBar;
		protected var hScroll:Boolean;
		protected var vScroll:Boolean;
		protected var _vScrollPolicy:String;
		protected var _hScrollPolicy:String;
		public static var defaultStyles:Object = {skin:"skins.ScrollPane_Skin", padding:0};
		
		public function ScrollPane()
		{
			return;
		}// end function
		
		public function get contentHeight() : Number
		{
			return this._contentHeight;
		}// end function
		
		public function get availableWidth() : Number
		{
			this.calculateAvailableSize();
			return this._availableWidth;
		}// end function
		
		public function set contentHeight(param1:Number) : void
		{
			this._contentHeight = param1;
			return;
		}// end function
		
		override public function set enabled(param1:Boolean) : void
		{
			if (this.enabled == param1)
			{
				return;
			}
			this._vScrollBar.enabled = param1;
			this._hScrollBar.enabled = param1;
			super.enabled = param1;
			return;
		}// end function
		
		public function get hLineScrollSize() : Number
		{
			return this._hScrollBar.lineScrollSize;
		}// end function
		
		protected function drawLayout() : void
		{
			this.calculateAvailableSize();
			this.calculateContentWidth();
			this.background.width = width;
			this.background.height = height;
			var _loc_1:* = Number(getStyleValue("padding"));
			if (this.vScroll)
			{
				this._vScrollBar.visible = true;
				this._vScrollBar.x = width - ScrollBar.WIDTH - _loc_1;
				this._vScrollBar.y = _loc_1;
				this._vScrollBar.height = this._availableHeight;
			}
			else
			{
				this._vScrollBar.visible = false;
			}
			this._vScrollBar.setScrollProperties(this._availableHeight, 0, this._contentHeight - this._availableHeight, this.vPageScrollSize);
			this.setVScrollPosition(this._vScrollBar.scrollPosition);
			if (this.hScroll)
			{
				this._hScrollBar.visible = true;
				this._hScrollBar.x = _loc_1;
				this._hScrollBar.y = height - ScrollBar.WIDTH - _loc_1;
				this._hScrollBar.width = this._availableWidth;
			}
			else
			{
				this._hScrollBar.visible = false;
			}
			this._hScrollBar.setScrollProperties(this._availableWidth, 0, this.contentWidth - this._availableWidth, this.hPageScrollSize);
			this.setHScrollPosition(this._hScrollBar.scrollPosition);
			this.disabledOverlay.setSize(width, height);
			this.disabledOverlay.setPosition(0, 0);
			this.contentScrollRect = this.contentContainer.scrollRect;
			this.contentScrollRect.width = this._availableWidth;
			this.contentScrollRect.height = this._availableHeight;
			this.contentContainer.scrollRect = this.contentScrollRect;
			this.contentContainer.y = _loc_1;
			this.contentContainer.x = _loc_1;
			return;
		}// end function
		
		protected function handleWheel(event:MouseEvent) : void
		{
			if (!this.enabled || !this._vScrollBar.visible || this._contentHeight <= this._availableHeight)
			{
				return;
			}
			this._vScrollBar.scrollPosition = this._vScrollBar.scrollPosition - event.delta * this.vLineScrollSize;
			this.setVScrollPosition(this._vScrollBar.scrollPosition);
			dispatchEvent(new ScrollEvent(BUIConstants.VERTICAL, event.delta, this.vScrollPosition));
			return;
		}// end function
		
		public function set hLineScrollSize(param1:Number) : void
		{
			this._hScrollBar.lineScrollSize = param1;
			return;
		}// end function
		
		protected function handleScroll(event:ScrollEvent) : void
		{
			if (event.target == this._vScrollBar)
			{
				this.setVScrollPosition(event.position);
			}
			else
			{
				this.setHScrollPosition(event.position);
			}
			return;
		}// end function
		
		public function get vLineScrollSize() : Number
		{
			return this._vScrollBar.lineScrollSize;
		}// end function
		
		public function get maxHScrollPosition() : Number
		{
			drawNow();
			return Math.max(0, this.contentWidth - this._availableWidth);
		}// end function
		
		protected function calculateAvailableSize() : void
		{
			var _loc_1:* = Number(getStyleValue("padding"));
			var _loc_2:* = height - _loc_1 * 2;
			if (this._vScrollPolicy == ScrollPolicy.ON || this._vScrollPolicy == ScrollPolicy.AUTO)
			{
				this.vScroll = this._contentHeight > _loc_2;
			}
			var _loc_3:* = width - (this.vScroll ? (ScrollBar.WIDTH) : (0)) - _loc_1 * 2;
			if (this._hScrollPolicy == ScrollPolicy.ON || this._hScrollPolicy == ScrollPolicy.AUTO)
			{
				this.hScroll = this.contentWidth > _loc_3;
			}
			if (this.hScroll)
			{
				_loc_2 = _loc_2 - ScrollBar.WIDTH;
			}
			if (this.hScroll && !this.vScroll && this._vScrollPolicy == ScrollPolicy.AUTO && this._contentHeight > _loc_2)
			{
				this.vScroll = true;
				_loc_3 = _loc_3 - ScrollBar.WIDTH;
			}
			this._availableHeight = _loc_2;
			this._availableWidth = _loc_3;
			return;
		}// end function
		
		public function get hScrollPosition() : Number
		{
			return this._hScrollBar.scrollPosition;
		}// end function
		
		public function get maxVScrollPosition() : Number
		{
			drawNow();
			return Math.max(0, this._contentHeight - this._availableHeight);
		}// end function
		
		override protected function initUI() : void
		{
			ScrollBar.WIDTH = 16;
			super.initUI();
			setSize(100, 100);
			this.contentScrollRect = new Rectangle(0, 0, 84, 84);
			this.background = new BSkin();
			addChild(this.background);
			this._vScrollBar = new ScrollBar();
			this._vScrollBar.addEventListener(ScrollEvent.SCROLL, this.handleScroll, false, 0, true);
			this._vScrollBar.visible = false;
			this._vScrollBar.lineScrollSize = this.defaultLineScrollSize;
			addChild(this._vScrollBar);
			this._hScrollBar = new ScrollBar();
			this._hScrollBar.orientation = BUIConstants.HORIZONTAL;
			this._hScrollBar.addEventListener(ScrollEvent.SCROLL, this.handleScroll, false, 0, true);
			this._hScrollBar.visible = false;
			this._hScrollBar.lineScrollSize = this.defaultLineScrollSize;
			addChild(this._hScrollBar);
			this.contentContainer = new Sprite();
			addChild(this.contentContainer);
			this.contentContainer.scrollRect = this.contentScrollRect;
			this.disabledOverlay = new BSkin();
			addChild(this.disabledOverlay);
			this._hScrollPolicy = ScrollPolicy.AUTO;
			this._vScrollPolicy = ScrollPolicy.AUTO;
			return;
		}// end function
		
		public function get vScrollPosition() : Number
		{
			return this._vScrollBar.scrollPosition;
		}// end function
		
		public function set vLineScrollSize(param1:Number) : void
		{
			this._vScrollBar.lineScrollSize = param1;
			return;
		}// end function
		
		public function get vScrollBar() : ScrollBar
		{
			return this._vScrollBar;
		}// end function
		
		public function set hScrollPosition(param1:Number) : void
		{
			drawNow();
			this._hScrollBar.scrollPosition = param1;
			this.setHScrollPosition(this._hScrollBar.scrollPosition);
			return;
		}// end function
		
		override public function get enabled() : Boolean
		{
			return super.enabled;
		}// end function
		
		override protected function draw() : void
		{
			if (isInvalid(Invalidation.STYLES))
			{
				this.drawStyles();
			}
			if (isInvalid(Invalidation.STATE))
			{
				this.drawDisabledOverlay();
			}
			if (isInvalid(Invalidation.SIZE))
			{
				this.drawLayout();
			}
			this.updateChildren();
			super.draw();
			return;
		}// end function
		
		public function update() : void
		{
			var _loc_1:* = this.contentContainer.getChildAt(0);
			this.setContentSize(_loc_1.width, _loc_1.height);
			return;
		}// end function
		
		public function set hPageScrollSize(param1:Number) : void
		{
			this._hPageScrollSize = param1;
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		protected function updateChildren() : void
		{
			this._hScrollBar.enabled = this.enabled;
			this._vScrollBar.enabled = this.enabled;
			this._vScrollBar.drawNow();
			this._hScrollBar.drawNow();
			this.background.drawNow();
			this.disabledOverlay.drawNow();
			return;
		}// end function
		
		public function set hScrollPolicy(param1:String) : void
		{
			this._hScrollPolicy = param1;
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		protected function calculateContentWidth() : void
		{
			return;
		}// end function
		
		public function get availableHeight() : Number
		{
			this.calculateAvailableSize();
			return this._availableHeight;
		}// end function
		
		public function set vScrollPosition(param1:Number) : void
		{
			drawNow();
			this._vScrollBar.scrollPosition = param1;
			this.setVScrollPosition(this._vScrollBar.scrollPosition);
			return;
		}// end function
		//设置大小
		protected function setContentSize(param1:Number, param2:Number) : void
		{
			if (this.contentWidth == param1 && this._contentHeight == param2)
			{
				return;
			}
			this.contentWidth = param1;
			this._contentHeight = param2;
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		public function get hPageScrollSize() : Number
		{
			if (isNaN(this._availableWidth))
			{
				drawNow();
			}
			if (this._hPageScrollSize == 0)
			{
			}
			return !isNaN(this._availableWidth) ? (this._availableWidth) : (this._hPageScrollSize);
		}// end function
		
		public function set vPageScrollSize(param1:Number) : void
		{
			this._vPageScrollSize = param1;
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		public function get hScrollPolicy() : String
		{
			return this._hScrollPolicy;
		}// end function
		
		public function set vScrollPolicy(param1:String) : void
		{
			this._vScrollPolicy = param1;
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		protected function setHScrollPosition(param1:Number) : void
		{
			var _loc_2:* = this.contentContainer.scrollRect;
			_loc_2.x = param1;
			this.contentContainer.scrollRect = _loc_2;
			return;
		}// end function
		
		protected function setVScrollPosition(param1:Number) : void
		{
			var _loc_2:* = this.contentContainer.scrollRect;
			_loc_2.y = param1;
			this.contentContainer.scrollRect = _loc_2;
			return;
		}// end function
		
		public function set content(param1:DisplayObject) : void
		{
			if (this.contentContainer.numChildren > 0)
			{
				this.contentContainer.removeChildAt(0);
			}
			this.contentContainer.addChild(param1);
			this.update();
			return;
		}// end function
		
		public function get vPageScrollSize() : Number
		{
			if (isNaN(this._availableHeight))
			{
				drawNow();
			}
			if (this._vPageScrollSize == 0)
			{
			}
			return !isNaN(this._availableHeight) ? (this._availableHeight) : (this._vPageScrollSize);
		}// end function
		
		public function get vScrollPolicy() : String
		{
			return this._vScrollPolicy;
		}// end function
		
		public function get content() : DisplayObject
		{
			if (this.contentContainer.numChildren > 0)
			{
				return this.contentContainer.getChildAt(0);
			}
			return null;
		}// end function
		
		override public function get classStyles() : Object
		{
			return mergeStyles(BUI.defaultStyles, defaultStyles);
		}// end function
		
		protected function drawDisabledOverlay() : void
		{
			this.disabledOverlay.visible = !this.enabled;
			return;
		}// end function
		
		public function get hScrollBar() : ScrollBar
		{
			return this._hScrollBar;
		}// end function
		
		protected function drawStyles() : void
		{
			var _loc_1:* = getSkinInstance(getStyleValue("skin"));
			//this.background.setStyle("skin", _loc_1["background"]);
			//this.disabledOverlay.setStyle("skin", _loc_1["disabledOverlay"]);
			return;
		}// end function
		
	}
}
